/**
 * sns-贴吧统计
 */
;
(function($, window, document, undefined) {
    var TiebaStatistic = function(el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons'
        };
        this.options = $.extend({}, this.defaults, opts);
    }

    TiebaStatistic.prototype = {
        init: function() {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == 'true') {
                return me;
            }

            me.initChart();
            me.bindEvents();


            el.attr('data-initialized', 'true');
            return me;
        },


        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function() {
            var me = this,
                el = me.el;

        },

        initChart: function() {
            var me = this,
                el = me.el,
                option;


            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);
        }
    }

    $.fn.TiebaStatistic = function(opts) {
        var me = this,
            el = me.el,
            comTiebaStatistic;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comTiebaStatistic = new TiebaStatistic(this, opts);
        }

        return comTiebaStatistic.init();
    }
})(jQuery, window, document);



/**
 * 传播力-报道统计
 */
;
(function($, window, document, undefined) {
    var comChart;

    var SNSTiebaStatistic = function(el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons',
            chartOption: {
                calculable: true,
                tooltip: {
                    trigger: 'axis'
                },
                dataZoom: {
                    show: true,
                    realtime: true,
                    start: 0,
                    end: 100
                },
                legend: {
                    data: ['帖子数', '粉丝数']
                },
                xAxis: [{
                    type: 'category',
                    data: []
                }],
                yAxis: [{
                    type: 'value'
                }],
                series: [{
                    name: '评论数',
                    type: 'bar',
                    data: []
                }, {
                    name: '赞数',
                    type: 'bar',
                    data: []
                }]
            }
        };
        this.options = $.extend({}, this.defaults, opts);
    };

    SNSTiebaStatistic.prototype = {
        init: function() {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == 'true') {
                return me;
            }

            me.initChart();
            me.bindEvents();


            el.attr('data-initialized', 'true');
            return me;
        },


        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function() {
            var me = this,
                el = me.el;

        },


        /**
         * 格式化数据
         * @param  {[type]} data [description]
         * @return {[type]}      [description]
         */
        formatData: function(originData) {
            var me = this,
                el = me.el,
                chartOption = me.options.chartOption,
                keys = ['post_count', 'fans_count'],
                i, i, len1, len2;


            // 初始化xaxis
            for (i = 0, len1 = originData.length; i < len1; i++) {
                chartOption.xAxis[0].data.push(originData[i]['date']);
            }

            // 初始化series
            $.each(keys, function(i, key) {
                chartOption.series[i].data = [];
            });
            for (i = 0, len1 = originData.length; i < len1; i++) {
                for (j = 0, len2 = keys.length; j < len2; j++) {
                    chartOption.series[j].data.push(originData[i][keys[j]]);
                }
            }


            return chartOption;
        },

        /**
         * [initChart description]
         * @return {[type]} [description]
         */
        initChart: function() {
            var me = this,
                el = me.el,
                option = {};

            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);

//            $.ajax({
//                url: $.fn.utils.env == 'dev' ? '/keywords/1/sns_tieba_statistic.json' : '',
//                type: 'GET',
//                dataType: 'json',
//                data: {},
//                success: function(ret) {
//                    if (ret && ret.status) {
//                        var data = ret.data[0].sns_tieba_statistic;
//
//                        option = me.formatData(data);
//                        comChart.setOption(option);
//                    } else {
//
//                    }
//                },
//                error: function(ret) {
//
//                }
//            });
        }
    };

    $.fn.SNSTiebaStatistic = function(opts) {
        var me = this,
            el = me.el,
            comSNSTiebaStatistic;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comSNSTiebaStatistic = new SNSTiebaStatistic(this, opts);
        }

        return comSNSTiebaStatistic.init();
    }
})(jQuery, window, document);
